package api

import (
	"api-server/basic/config"
	"api-server/handler/request"
	"api-server/proto/user"
	"context"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"time"
)

func SendSms(c *gin.Context) {
	var req request.SendSmsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10001,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	// Contact the server and print out its response.
	_, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	_, err := config.ApiClient.SendSms(c, &user.SendSmsReq{
		Mobile: req.Mobile,
		Sours:  req.Sours,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "验证码发送成功",
		"date": user.SendSmsResp{},
	})
}

func SmsLogin(c *gin.Context) {
	var req request.SmsSendReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10001,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	// Contact the server and print out its response.
	_, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	_, err := config.ApiClient.SmsLogin(c, &user.SmsLoginReq{
		Mobile:   req.Mobile,
		Password: req.Password,
		Sours:    req.Sours,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"date": user.SendSmsResp{},
	})
}
